<?php

/**
 * Description of SignupService
 *
 * @author Adam
 */
class LogService {

    private static $instance = null;

    public static function getInstance() {
        if (!self::$instance instanceof self) {
            self::$instance = new self;
        }
        return self::$instance;
    }

    public function __destruct() {
        
    }

    public function getuserLog($DBprefix, $idUser, $sesId) {
        $idUser = mysql_escape_string($idUser);
        $sesId = mysql_escape_string($sesId);
        $query = "SELECT * FROM `" . $DBprefix . "logs` WHERE `log_userid` = '$idUser' AND `log_sesid` = '" . $sesId . "'";
        $queryResult = mysql_query($query);

        $log = new Log();
        if ($logArr = mysql_fetch_array($queryResult)) {
            $log->setId($logArr["log_id"]);
            $log->setIp($logArr["log_ip"]);
            $log->setDate($logArr["log_date"]);
            $log->setStatus($logArr["log_status"]);
            $log->setSesExp($logArr["log_sesexp"]);
            $log->setSesId($logArr["log_sesid"]);
            $log->setAction($logArr["log_action"]);
            $log->setPageTo($logArr["log_page_to"]);
            $log->setPageFrom($logArr["log_page_from"]);
        }

        return $log;
    }

    public function add($DBprefix, Log $log) {
        $query = "INSERT INTO `" . $DBprefix . "logs` (`log_action`,`log_userid`,`log_page_to`,`log_page_from`,`log_ip`,`log_date`, `log_status`,`log_sesid`) VALUES " .
                "('" . $log->getAction() . "','" . $log->getUserId() . "','" . $log->getPageTo() . "','" . $log->getPageFrom() . "','" . $log->getIp() . "','" . $log->getDate() . "', '" . $log->getStatus() . "','" . $log->getSesId() . "')";
        mysql_query($query) or die;
    }
    
    public function find($DBprefix,$info_user_id,$log_sesid,$action,$log_sesexp) {
        $query = "SELECT * FROM `" . $DBprefix . "logs` WHERE `log_userid` = '$info_user_id' " .
                "AND `log_sesid` = '$log_sesid' AND `log_action` = '$action' AND `log_sesexp` = '$log_sesexp'";
        $queryResult = mysql_query($query);

        $log = new Log();
        if ($logArr = mysql_fetch_array($queryResult)) {
            $log->setId($logArr["log_id"]);
            $log->setIp($logArr["log_ip"]);
            $log->setDate($logArr["log_date"]);
            $log->setStatus($logArr["log_status"]);
            $log->setSesExp($logArr["log_sesexp"]);
            $log->setSesId($logArr["log_sesid"]);
            $log->setAction($logArr["log_action"]);
            $log->setPageTo($logArr["log_page_to"]);
            $log->setPageFrom($logArr["log_page_from"]);
        }

        return $log;
    }
    
    public function getNumRows($DBprefix,$logAction,$orderBy) {
        $query = "SELECT * FROM `".$DBprefix."logs` WHERE `log_action` LIKE '$logAction' ORDER BY `$orderBy` DESC"; 
        $result = mysql_query($query);
        $num = mysql_num_rows($result);
        return $num;
    }
    
    public function getUserNumRows($DBprefix,$logAction,$userId,$orderBy) {
        $query = "SELECT * FROM `".$DBprefix."logs` WHERE `log_action` LIKE 'LOGIN' AND `log_userid` = '$userId' ORDER BY `log_id`"; 
        $result = mysql_query($query);
        $num = mysql_num_rows($result);
        return $num;
    }
    
    public function getUserActiveCount($DBprefix,$logStatus,$logAction) {
        $query = "SELECT * FROM `".$DBprefix."logs` WHERE `log_status` = '$logStatus' AND `log_action` = '$logAction'"; 
        $result = mysql_query($query);
        $num = mysql_num_rows($result);
        return $num;
    }
    
    public function getNumRowsLimit($DBprefix,$logAction,$orderBy,$begin,$rowsOnPage) {
        $query = "SELECT * FROM `".$DBprefix."logs` WHERE `log_action` LIKE '$logAction' ORDER BY `$orderBy` DESC LIMIT $begin,$rowsOnPage"; 
        $result = mysql_query($query);
        $num = mysql_num_rows($result);
        return $num;
    }
    
    public function getUserLogLimit($DBprefix,$logAction,$orderBy,$begin,$rowsOnPage) {
        $query = "SELECT * FROM `".$DBprefix."logs` WHERE `log_action` LIKE '$logAction' ORDER BY `$orderBy` DESC LIMIT $begin,$rowsOnPage"; 
        $result = mysql_query($query);
        
        $logs = array();
        while ($logArr = mysql_fetch_array($result)){
            $log = new Log();
            $log->setId($logArr["log_id"]);
            $log->setIp($logArr["log_ip"]);
            $log->setDate(date("d.m.Y h:iA",$logArr["log_date"]));
            $log->setStatus($logArr["log_status"]);
            $log->setSesExp($logArr["log_sesexp"]);
            $log->setSesId($logArr["log_sesid"]);
            $log->setAction($logArr["log_action"]);
            $log->setPageTo($logArr["log_page_to"]);
            $log->setPageFrom($logArr["log_page_from"]);
            $log->setUserId($logArr["log_userid"]);
            $log->setInDate($logArr["log_indate"]);
            $log->setOutDate($logArr["log_outdate"]);
            $logs[] = $log;
        }
        
        return $logs;
    }
    
    public function getLogLimitByUserIdAndAction($DBprefix,$logAction,$userId,$orderBy,$begin,$rowsOnPage) {
        $query = "SELECT * FROM `".$DBprefix."logs` WHERE `log_action` LIKE '$logAction' AND `log_userid` = '$userId' ORDER BY `$orderBy` DESC LIMIT $begin,$rowsOnPage";
        $result = mysql_query($query);
        
        $logs = array();
        while ($logArr = mysql_fetch_array($result)){
            $log = new Log();
            $log->setId($logArr["log_id"]);
            $log->setIp($logArr["log_ip"]);
            $log->setDate(date("d.m.Y h:iA",$logArr["log_date"]));
            $log->setStatus($logArr["log_status"]);
            $log->setSesExp($logArr["log_sesexp"]);
            $log->setSesId($logArr["log_sesid"]);
            $log->setAction($logArr["log_action"]);
            $log->setPageTo($logArr["log_page_to"]);
            $log->setPageFrom($logArr["log_page_from"]);
            $log->setUserId($logArr["log_userid"]);
            $log->setInDate($logArr["log_indate"]);
            $log->setOutDate($logArr["log_outdate"]);
            $logs[] = $log;
        }
        
        return $logs;
    }
    
    public function getLogBySessionId($DBprefix,$sessionId) {
        $query = "SELECT * FROM `".$DBprefix."logs` WHERE `log_action` LIKE '$logAction' AND `log_userid` = '$userId' ORDER BY `$orderBy` DESC LIMIT $begin,$rowsOnPage";
        $result = mysql_query($query);
        
        $logs = array();
        while ($logArr = mysql_fetch_array($result)){
            $log = new Log();
            $log->setId($logArr["log_id"]);
            $log->setIp($logArr["log_ip"]);
            $log->setDate(date("d.m.Y h:iA",$logArr["log_date"]));
            $log->setStatus($logArr["log_status"]);
            $log->setSesExp($logArr["log_sesexp"]);
            $log->setSesId($logArr["log_sesid"]);
            $log->setAction($logArr["log_action"]);
            $log->setPageTo($logArr["log_page_to"]);
            $log->setPageFrom($logArr["log_page_from"]);
            $log->setUserId($logArr["log_userid"]);
            $log->setInDate($logArr["log_indate"]);
            $log->setOutDate($logArr["log_outdate"]);
            $logs[] = $log;
        }
        
        return $logs;
    }
    
    public function getLogCountBySessionId($DBprefix,$sessionId) {
        $query = "SELECT * FROM `".$DBprefix."logs` WHERE `log_action` LIKE '$logAction' AND `log_userid` = '$userId' ORDER BY `$orderBy` DESC LIMIT $begin,$rowsOnPage";
        $result = mysql_query($query);
        $count = mysql_num_rows($result);
        
        return $count;
    }

    
    public function save($DBprefix, Log $log) {
        $query = "UPDATE ".$DBprefix."logs SET `log_sesexp` = '".$log->getSesExp()."' WHERE `log_id` = '".$log->getId()."'";  
        mysql_query($query);
    }
}

?>
